import { Editor, Path, Point, Range } from 'slate';

/**
 * 当前文档是否进行了范围选中
 * true ==> 没有
 * false ==> 范围选中
 * @param editor
 * @param at
 * @returns
 */
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
export const isCollapsed = (editor: Editor, at: Range = editor.selection!): boolean => {
  /**
   * 这里的 !at 为了防止 at 为 空
   * **/
  return !at || Range.isCollapsed(at);
};
/**
 * 是否符合按键的条件
 * @param event
 * @param keys
 * @returns
 */
export const isMatchKeyDown = (event: KeyboardEvent, ...keys: string[]): boolean => {
  const key = event.key;
  const code = event.code;

  return keys.indexOf(key) > -1 || keys.indexOf(code) > -1;
};

/**
 * 判断当前光标是否在当前行的末尾
 * @param editor
 * @param path
 * @returns
 */
export const isFouceEnd = (editor: Editor, path: Path) => {
  const end = Editor.end(editor, path);
  if (!editor.selection) throw new Error('editor.selection is undefind');
  return Point.equals(end, editor.selection.focus);
};

/**
 * 判断当前光标是否在当前行的开头
 * @param editor
 * @param path
 * @returns
 */
export const isFouceStart = (editor: Editor, path: Path) => {
  const start = Editor.start(editor, path);
  if (!editor.selection) throw new Error('editor.selection is undefind');

  return Point.equals(start, editor.selection.focus);
};
